using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Automation;
using System.Collections;
using System.Configuration;
using HyperVSnapin.Object;
using HyperVSnapin.Helpers;

namespace HyperVSnapin.Command
{
    [Cmdlet(VerbsCommon.Get, "VHost", SupportsShouldProcess = true)]
    public class GetVHostCommand : Cmdlet
    {

        #region Parameters
        [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Computer name to connect to")]
        [ValidateNotNullOrEmpty]
        public string[] ComputerName
        {
            get;
            set;
        }

        [Parameter(Mandatory = false, HelpMessage = "Credential if not those of current user")]
        [Credential]
        public PSCredential Credentials
        {
            get;
            set;
        }
        #endregion

        protected override void ProcessRecord()
        {
            try
            {
                HostWMIHelper helper = new HostWMIHelper();
                List<Host> results = new List<Host>();
                foreach (string name in ComputerName)
	            {   
                    try
                    {
                        List<ManagementObject> hosts = new List<ManagementObject>();
                        hosts.AddRange(helper.GetHost(name, this.Credentials));
                        foreach (ManagementObject mo in hosts)
                        {
                            results.Add(new Host(mo, name, this.Credentials));
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex,"GetHyperVServerCommand, Getting Host: " + ex, ErrorCategory.NotSpecified,null));
                    }            		 
	            }

                WriteObject(results, true);
            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(ex, "GetHyperVServerCommand" + ex, ErrorCategory.NotSpecified, null));
            }
        }
    }
}
